package com.huage.courseplatform.application.config;

import com.huage.courseplatform.framework.Interceptor.RepeatSubmitInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {

    /**
     * 重复提交拦截器
     */
    @Resource
    private RepeatSubmitInterceptor repeatSubmitInterceptor;

    /**
     * 放行文件目录
     */
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //获取文件的真实路径
        String path = System.getProperty("user.dir");
        log.info("uploadFilePath:"+System.getProperty("user.dir")+"\\file");
        //registry.excludePathPatterns()
        registry.addResourceHandler("/img/**").addResourceLocations("file:"+path+"/upload/img/");
        registry.addResourceHandler("/video/**").addResourceLocations("file:"+path+"/upload/video/");

        //获取文件的真实路径
//        String path = System.getProperty("user.dir")+"/upload/img/";
//        log.info("uploadFilePath:"+path);
//        registry.addResourceHandler("/img/**")
//                .addResourceLocations("file:"+path);

    }

    /**
     * 添加重复提交拦截器
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(repeatSubmitInterceptor).addPathPatterns("/**");
    }
}
